/**
 * @Description: 权限控制（按钮级）
 * 仅根据后端返回的 buttonPermissions 生成布尔表，菜单走后端 menus
 */

export type AccessInstance = Record<string, boolean>;

export default (initialState: any): AccessInstance => {
  const { currentUser } = initialState || {};

  if (!currentUser) {
    return {} as AccessInstance;
  }

  const buttonPermissions: string[] = currentUser.buttonPermissions || [];

  const accessPermissions: Record<string, boolean> = {};
  buttonPermissions.forEach((permissionKey) => {
    accessPermissions[permissionKey] = true;
  });

  const canSeeAdmin = !!(
    currentUser && currentUser.userName !== 'dontHaveAccess'
  );

  return {
    ...accessPermissions,
    canSeeAdmin,
  } as AccessInstance;
};
